package com.ctg.itrdc.imageconsole.control.imageconfig;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconfig.service.IImageConfigService;
import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.imageconfig.ImageConfigDto;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfig;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfigName;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfigVersion;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;

/**
 * 配置文件接口
 * 
 * @author yuxiaoyu
 *
 */
@Controller
@RequestMapping("/config")
public class ImageConfigController {

	@Resource
	private IImageConfigService imageConfigService;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@RequestMapping(value = "/queryImageConfigList", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryImageConfigList(@RequestParam("dockerImageTagId") Long dockerImageTagId,
			@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
		try {
			PageInfo<ImageConfig> pageInfo = imageConfigService.jdbcFindPageInfo(dockerImageTagId, pageNum, pageSize);
			if (pageInfo.getList().size() > 0) {
				return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfo);
			}
			return new CommonResult(ReturnCodeDefine.Code.C1101, ReturnCodeDefine.Msg.M1101);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}

	}

	@RequestMapping(value = "/addImageConfig", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult addImageConfig(@RequestBody ImageConfigDto image) {

		try

		{
			imageConfigService.addImageConfig(image);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}

	}

	@RequestMapping(value = "/updateImageConfig", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updateImageConfig(@RequestBody ImageConfigDto image) {

		try {
			imageConfigService.updateByPrimaryKey(image);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}

	}

	@RequestMapping(value = "/delImageConfig", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult delImageConfig(@RequestParam("id") Long id) {

		try {
			imageConfigService.delImageConfig(id);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}

	}

	@RequestMapping(value = "/getImageConfigInfo", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getImageConfigInfo(@RequestParam("id") Long id) {
		try {
			ImageConfig imageConfig = new ImageConfig();
			imageConfig.setId(id);
			ImageConfig imageConfigDto = imageConfigService.selectByPrimaryKey(imageConfig);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, imageConfigDto);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/checkImageConfigVersion", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult checkImageConfigVersion(@RequestParam("dockerImageTagId") Long dockerImageTagId,
			@RequestParam("configVersion") String configVersion,@RequestParam("config_name") String config_name) {
		try {
			boolean bol = imageConfigService.checkImageConfigVersion(dockerImageTagId, configVersion,config_name);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, bol);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/queryImageConfigNameByImageId", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryImageConfigNameByImageId(@RequestParam("dockerImageTagId") Long dockerImageTagId) {
		try {
			List<ImageConfigName> list = imageConfigService.queryImageConfigNameByImageId(dockerImageTagId);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/queryImageConfigVersion", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryImageConfigVersion(@RequestParam("dockerImageTagId") Long dockerImageTagId,
			@RequestParam("config_name") String config_name) {
		try {
			List<ImageConfigVersion> list = imageConfigService.queryImageConfigVersion(dockerImageTagId, config_name);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/queryImageConfigByVersion", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryImageConfigByVersion(@RequestParam("dockerImageTagId") Long dockerImageTagId,
			@RequestParam("config_name") String config_name, @RequestParam("config_version") String config_version) {
		try {
			List<ImageConfig> list = imageConfigService.queryImageConfigByVersion(dockerImageTagId, config_name,
					config_version);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

	@RequestMapping(value = "/queryImageConfigByName", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryImageConfigByName(@RequestParam(value="config_name",required=false) String configName,
			@RequestParam("dockerImageTagId") Long dockerImageTagId, @RequestParam("pageNum") int pageNum,
			@RequestParam("pageSize") int pageSize) {
		try {
			PageInfo<ImageConfig> pageInfo = imageConfigService.jdbcFindPageInfo(configName, dockerImageTagId, pageNum,
					pageSize);
			if (pageInfo.getList().size() > 0) {
				return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, pageInfo);
			}
			return new CommonResult(ReturnCodeDefine.Code.C1101, ReturnCodeDefine.Msg.M1101);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage());
		}
	}

}
